use crate::{command::Command, error::CompileError};
 
 
// 修改运行函数接口: 读取指令部分被解耦开
pub fn compile(lines: &Vec<(usize, String)>) -> Result<Vec<u16>, CompileError> {
    // 将字符串读取为Cmd中间数据结构
    let mut cmds: Vec<Command> = Vec::new();
    // 将字符串指令汇编成机器指令
    let mut codes: Vec<u16> = Vec::new();
    for line in lines {

        let stream:Vec<&str> = line.1.splitn( 2," " ).collect();

        let op = stream[0];
        let params = stream.get(1);
        let cmd = Command::parse(op, params)
            .or_else(|err| Err(CompileError::new(line.0, err)))?;
  
        let code = cmd.to_code().map_err(|kind| CompileError::new(line.0, kind))?;
        codes.push(code);
        cmds.push(cmd);
    }


    Ok(codes)
}